<template>
  <div class="order-container">
    <h3>我的订单</h3>

    <!-- 订单状态标签 -->
    <div class="order-tabs">
      <div
          v-for="status in orderStatuses"
          :key="status.value"
          class="tab-item"
          :class="{ active: activeStatus === status.value }"
          @click="changeStatus(status.value)"
      >
        {{ status.label }}
      </div>
    </div>

    <!-- 订单列表 -->
    <div class="order-list">
      <div
          class="order-card"
          v-for="order in filteredOrders"
          :key="order.id"
      >
        <div class="order-header">
          <span class="order-no">订单号: {{ order.orderNo }}</span>
          <span class="order-status" :class="getStatusClass(order.status)">
            {{ getStatusText(order.status) }}
          </span>
        </div>

        <div class="order-body">
          <div class="product-list">
            <div
                class="product-item"
                v-for="item in order.products"
                :key="item.id"
            >
              <img :src="item.image" alt="商品图片" class="product-img">
              <div class="product-info">
                <h3 class="product-name">{{ item.name }}</h3>
                <p class="product-spec">{{ item.spec }}</p>
              </div>
              <div class="product-price">
                <p>¥{{ item.price }}</p>
                <p class="product-quantity">x{{ item.quantity }}</p>
              </div>
            </div>
          </div>
        </div>

        <div class="order-footer">
          <div class="order-total">
            共 {{ order.products.length }} 件商品 合计: ¥{{ order.totalAmount }}
          </div>
          <div class="order-actions">
            <button
                v-if="order.status === 'pending_payment'"
                class="btn btn-primary"
                @click="payOrder(order.id)"
            >
              立即支付
            </button>
            <button
                v-if="order.status === 'pending_receipt'"
                class="btn btn-confirm"
                @click="confirmReceipt(order.id)"
            >
              确认收货
            </button>
            <button
                class="btn btn-detail"
                @click="viewDetail(order.id)"
            >
              订单详情
            </button>
          </div>
        </div>
      </div>
    </div>

    <!-- 分页 -->
    <div class="pagination">
      <button
          class="page-btn"
          :disabled="currentPage === 1"
          @click="prevPage"
      >
        上一页
      </button>
      <span class="page-info">
        第 {{ currentPage }} 页 / 共 {{ totalPages }} 页
      </span>
      <button
          class="page-btn"
          :disabled="currentPage === totalPages"
          @click="nextPage"
      >
        下一页
      </button>
    </div>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue'

// 订单状态选项
const orderStatuses = ref([
  { label: '全部', value: 'all' },
  { label: '待付款', value: 'pending_payment' },
  { label: '待发货', value: 'pending_shipment' },
  { label: '待收货', value: 'pending_receipt' },
  { label: '已完成', value: 'completed' }
])

// 当前选中的状态
const activeStatus = ref('all')

// 当前页码
const currentPage = ref(1)
const pageSize = 5

// 模拟订单数据
const orders = ref([
  {
    id: 1,
    orderNo: '20230601123456',
    status: 'pending_payment',
    totalAmount: 8999,
    products: [
      {
        id: 101,
        name: 'Apple iPhone 14 Pro Max',
        spec: '深空黑色 256GB',
        price: 8999,
        quantity: 1,
        image: 'src/assets/img/product/demo/iphone.png'
      }
    ]
  },
  {
    id: 2,
    orderNo: '20230602123457',
    status: 'pending_shipment',
    totalAmount: 5999,
    products: [
      {
        id: 102,
        name: '华为MateBook D14',
        spec: 'R5/16GB/512GB',
        price: 5999,
        quantity: 1,
        image: 'src/assets/img/product/demo/laptop.png'
      }
    ]
  },
  {
    id: 3,
    orderNo: '20230603123458',
    status: 'pending_receipt',
    totalAmount: 2999,
    products: [
      {
        id: 103,
        name: '小米手环7',
        spec: '标准版',
        price: 2999,
        quantity: 1,
        image: 'src/assets/img/product/demo/watch.png'
      }
    ]
  },
  {
    id: 4,
    orderNo: '20230604123459',
    status: 'completed',
    totalAmount: 1999,
    products: [
      {
        id: 104,
        name: '罗技G502鼠标',
        spec: 'RGB版',
        price: 1999,
        quantity: 1,
        image: 'src/assets/img/product/demo/mouse.png'
      }
    ]
  }
])

// 根据状态筛选订单
const filteredOrders = computed(() => {
  const start = (currentPage.value - 1) * pageSize
  const end = start + pageSize

  let result = orders.value
  if (activeStatus.value !== 'all') {
    result = result.filter(order => order.status === activeStatus.value)
  }

  return result.slice(start, end)
})

// 计算总页数
const totalPages = computed(() => {
  let count = orders.value.length
  if (activeStatus.value !== 'all') {
    count = orders.value.filter(order => order.status === activeStatus.value).length
  }
  return Math.ceil(count / pageSize)
})

// 切换订单状态
const changeStatus = (status) => {
  activeStatus.value = status
  currentPage.value = 1
}

// 分页操作
const prevPage = () => {
  if (currentPage.value > 1) currentPage.value--
}

const nextPage = () => {
  if (currentPage.value < totalPages.value) currentPage.value++
}

// 获取状态文本
const getStatusText = (status) => {
  const map = {
    pending_payment: '待付款',
    pending_shipment: '待发货',
    pending_receipt: '待收货',
    completed: '已完成'
  }
  return map[status] || status
}

// 获取状态样式类
const getStatusClass = (status) => {
  return `status-${status}`
}

// 订单操作
const payOrder = (id) => {
  console.log('支付订单:', id)
}

const confirmReceipt = (id) => {
  console.log('确认收货:', id)
}

const viewDetail = (id) => {
  console.log('查看详情:', id)
}
</script>

<style scoped>
h1 {
  text-align: left;
  color: #333;
  margin-bottom: 2rem;
  padding-bottom: 10px;
  border-bottom: 1px solid #f0f0f0;
}
.order-container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 20px;
}

/* 订单状态标签样式 */
.order-tabs {
  display: flex;
  border-bottom: 1px solid #eee;
  margin-bottom: 20px;
}

.tab-item {
  padding: 12px 20px;
  cursor: pointer;
  font-size: 14px;
  color: #666;
  position: relative;
  transition: all 0.3s;
}

.tab-item:hover {
  color: #007BFF;
}

.tab-item.active {
  color: #007BFF;
  font-weight: 500;
}

.tab-item.active::after {
  content: '';
  position: absolute;
  bottom: -1px;
  left: 0;
  right: 0;
  height: 2px;
  background-color: #007BFF;
}

.order-list {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.order-card {
  background: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.05);
  overflow: hidden;
  transition: box-shadow 0.3s;
}

.order-card:hover {
  box-shadow: 0 4px 12px rgba(0,0,0,0.1);
}

.order-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 15px 20px;
  background: #fafafa;
  border-bottom: 1px solid #f0f0f0;
}

.order-no {
  font-size: 14px;
  color: #666;
}

.order-status {
  padding: 4px 10px;
  border-radius: 12px;
  font-size: 12px;
  font-weight: 500;
}

.order-body {
  padding: 20px;
}

.product-list {
  display: flex;
  flex-direction: column;
  gap: 15px;
}

.product-item {
  display: flex;
  align-items: center;
  padding-bottom: 15px;
  border-bottom: 1px dashed #f0f0f0;
}

.product-item:last-child {
  border-bottom: none;
  padding-bottom: 0;
}

.product-img {
  width: 80px;
  height: 80px;
  object-fit: cover;
  border-radius: 4px;
  margin-right: 15px;
}

.product-info {
  flex: 1;
}

.product-name {
  font-size: 16px;
  margin-bottom: 5px;
  color: #333;
}

.product-spec {
  font-size: 12px;
  color: #999;
}

.product-price {
  text-align: right;
  font-size: 16px;
  color: #e53935;
  min-width: 100px;
}

.product-quantity {
  font-size: 12px;
  color: #999;
  margin-top: 5px;
}

.order-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 15px 20px;
  border-top: 1px solid #f0f0f0;
  background: #fafafa;
}

.order-total {
  font-size: 14px;
  color: #666;
}

.order-actions {
  display: flex;
  gap: 10px;
}

.btn {
  padding: 8px 16px;
  border-radius: 4px;
  cursor: pointer;
  font-size: 14px;
  transition: all 0.3s;
  border: 1px solid transparent;
}

.btn-primary {
  background: #1890ff;
  color: #fff;
}

.btn-primary:hover {
  background: #40a9ff;
}

.btn-confirm {
  background: #fff;
  color: #1890ff;
  border: 1px solid #1890ff;
}

.btn-confirm:hover {
  background: #e6f7ff;
}

.btn-detail {
  background: #fff;
  color: #666;
  border: 1px solid #d9d9d9;
}

.btn-detail:hover {
  background: #f5f5f5;
  border-color: #bfbfbf;
}

.pagination {
  display: flex;
  justify-content: center;
  align-items: center;
  margin-top: 30px;
  gap: 20px;
}

.page-btn {
  padding: 8px 20px;
  background: #fff;
  border: 1px solid #d9d9d9;
  border-radius: 4px;
  cursor: pointer;
  transition: all 0.3s;
}

.page-btn:hover:not(:disabled) {
  border-color: #1890ff;
  color: #1890ff;
}

.page-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.page-info {
  font-size: 14px;
  color: #666;
}
</style>